//
//  ZPwaihuShowView.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2017/6/14.
//  Copyright © 2017年 zjhcsoftios. All rights reserved.
//

#import "ZPwaihuShowView.h"
#import "ZPwaihuTableViewCell.h"
#import <MessageUI/MessageUI.h>
#import <MJExtension/MJExtension.h>
#import "ZPwaihuModel.h"

#define iSEESCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
@interface ZPwaihuShowView () <UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableHieght;

//@property (nonatomic, strong) NSIndexPath *selectIndexPath;

//2同时有2种电话 1只有一种电话
@property (nonatomic, assign) NSInteger sectionInTable;

@property (nonatomic, strong) ZPwaihuModel *yewuModel;

@property (nonatomic, strong) NSArray *phoneModelList;


@end

@implementation ZPwaihuShowView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


- (void)setYewuhaoma:(NSDictionary *)yewuhaoma {
    _yewuhaoma = yewuhaoma;
    _yewuModel = [ZPwaihuModel mj_objectWithKeyValues:_yewuhaoma];
}

- (void)setPhoneList:(NSArray *)phoneList {
    _phoneList = phoneList;
    
    _sectionInTable = 0;
    
    _phoneModelList = [ZPwaihuModel mj_objectArrayWithKeyValuesArray:_phoneList];
    
    if (_yewuModel.phone) {
        
        if (_phoneModelList.count > 0) {
            _sectionInTable = 2;
            _tableHieght.constant = 50 * 2 + 5 + 50 * (_phoneModelList.count + 1);
        }else {
            _tableHieght.constant = 50 + 50;
            _sectionInTable = 1;
        }
        
    }else {
        if (_phoneModelList.count > 0) {
            _sectionInTable = 1;
            _tableHieght.constant = 50 * (_phoneModelList.count + 1);
        }
    }
    
    [self.tableView reloadData];
}


- (void)awakeFromNib {
    [super awakeFromNib];
    [self setTableView];
}

- (void)setTableView {
    
    _sectionInTable = 0;

    [self.tableView registerNib:[UINib nibWithNibName:@"ZPwaihuTableViewCell" bundle:nil] forCellReuseIdentifier:@"ZPwaihuTableViewCellIdentify"];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:_tableView];
}


- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self dismiss];
//    [self removeFromSuperview];
}

#pragma mark table

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (_sectionInTable == 2 && section == 0) {
        return 5;
    }else {
        return 0;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return _sectionInTable;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    if (_sectionInTable == 2) {
        if (section == 0) {
            return 1;
        }else {
            return _phoneList.count;
        }
    }else if (_sectionInTable == 1) {
        if (_phoneList.count == 0) {
            return 1;
        }else {
            return _phoneList.count;
        }
    }else {
        return 0;
    }
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, iSEESCREEN_WIDTH - 20, 50)];
    if (_sectionInTable == 2) {
        if (section == 0) {
            cell.textLabel.text = @"业务号码";
        }else if (section == 1) {
            cell.textLabel.text = @"联系人号码";
        }
    }else if (_sectionInTable == 1) {
        if (_phoneModelList.count > 0) {
             cell.textLabel.text = @"联系人号码";
        }else {
            cell.textLabel.text = @"业务号码";
        }
    }
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, iSEESCREEN_WIDTH, 5)];
    view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
    return view;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZPwaihuTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZPwaihuTableViewCellIdentify" forIndexPath:indexPath];
    for (int i = 0; i < 5; i++) {
        UIImageView *imageView = (UIImageView *)[cell viewWithTag:i + 100];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(iconImageSelect:)];
        [imageView addGestureRecognizer:tap];
    }
    
    if (_sectionInTable == 2) {
        if (indexPath.section == 0) {
            cell.model = _yewuModel;
            cell.updateImageView.hidden = YES;
        }else if (indexPath.section == 1) {
            cell.model = _phoneModelList[indexPath.row];
            cell.updateImageView.hidden = NO;
        }
    }else if (_sectionInTable == 1) {
        if (_phoneModelList.count > 0) {
            cell.model = _phoneModelList[indexPath.row];
            cell.updateImageView.hidden = NO;
        }else {
            cell.model = _yewuModel;
            cell.updateImageView.hidden = YES;
        }
    }
    return cell;
}


#pragma mark 点击事件

- (void)iconImageSelect:(UITapGestureRecognizer *)tap {
    
    ZPwaihuTableViewCell *cell = (ZPwaihuTableViewCell *)tap.view.superview.superview;
    
    if ([self.delegate respondsToSelector:@selector(iconImageSelect:ImageTag:Remove:HeiFlag:)]) {
        
        if (cell.model.updateFlag) {
            
            [self.delegate iconImageSelect:cell.model.phone ImageTag:tap.view.tag - 100 Remove:@"remove" HeiFlag:cell.model.heiFlag];
        }else {
            [self.delegate iconImageSelect:cell.model.phone ImageTag:tap.view.tag - 100 Remove:@"add" HeiFlag:cell.model.heiFlag];
        }
        
    }
    
}


#pragma mark 显示方法
//
//+ (void)showWaihuViewWithPhoneList:(NSArray *)phoneList Yewuhaoma:(NSDictionary *)yewuhaoma Controller:(UIViewController *) baseController {
//    
//    ZPwaihuShowView *waihuView = [[NSBundle mainBundle] loadNibNamed:@"ZPwaihuShowView" owner:nil options:nil].lastObject;
//    waihuView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
//    waihuView.yewuhaoma = yewuhaoma;
//    waihuView.phoneList = phoneList;
//    waihuView.delegate = baseController;
//    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
//    [app.window addSubview:waihuView];
//    
//    [waihuView show];
//    
//    
//}


- (void)showWithView:(UIView *)view {
    [view addSubview:self];
    [self show];
    
}


- (void)show {
    
    [self layoutIfNeeded];
    self.cannelViewButtom.constant = 10;
    [UIView animateWithDuration:.25 animations:^{
        [self layoutIfNeeded];
    }];
}

- (void)dismiss {
    [self layoutIfNeeded];
    self.cannelViewButtom.constant = -self.tableHieght.constant - 10 - 10 - 50;
    [UIView animateWithDuration:.25 animations:^{
        [self layoutIfNeeded];
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}




@end
